package com.yunfeng.supercall.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import cn.jpush.android.api.InstrumentedActivity;

import com.lin.activity.LauncherActivity;
import com.yunfeng.supercall.R;
import com.yzx.api.UCSService;

/**
 * 欢迎页面
 * 
 * @author 洋
 * 
 */
public class WelcomeActivity extends InstrumentedActivity  implements AnimationListener {

	public static final int[] LOADING_RES = new int[] {
		R.drawable.ad_loading_1, R.drawable.ad_loading_2,
		R.drawable.ad_loading_3, R.drawable.ad_loading_4 };
	private ViewPager mViewPager;
	private ImageView imageViewSuperMan;

	/**
	 * 是否是第一次用
	 * 
	 * @return
	 */
	public boolean isFirstUse() {
		SharedPreferences mPreferences = getSharedPreferences("first",
				MODE_PRIVATE);
		boolean first = mPreferences.getBoolean("first", true);
		mPreferences.edit().putBoolean("first", false).commit();
		return first;
	}

	@SuppressLint("InlinedApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		if (!isFirstUse()) {
			if (UCSService.isConnected()) {
				ImageView iv = new ImageView(this);
				iv.setScaleType(ScaleType.CENTER_CROP);
				iv.setImageResource(R.drawable.ad_loading_1);
				setContentView(iv);
				startActivity(new Intent(this, MainActivity.class));
			} else {
				//				startActivity(new Intent(this, LoginActivity.class));
				startActivity(new Intent(this, LauncherActivity.class));
			}

			finish();
			return;
		}
		/**
		 * 4.4以上系统开启透明状态栏
		 */
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		}
		setContentView(R.layout.activity_welcome);

		imageViewSuperMan = (ImageView) findViewById(R.id.imageview_superman);
		imageViewSuperMan.setVisibility(View.INVISIBLE);
		mViewPager = (ViewPager) findViewById(R.id.view_pager);
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				Button button = (Button) findViewById(R.id.btn_delete);
				if (arg0 == 3) {
					if (button.getVisibility() == View.GONE) {
						button.startAnimation(AnimationUtils
								.loadAnimation(WelcomeActivity.this,
										R.anim.push_in_down_to_up));
						button.setVisibility(View.VISIBLE);
						imageViewSuperMan.setVisibility(View.VISIBLE);
					}
				} else {
					imageViewSuperMan.setVisibility(View.INVISIBLE);
					if (button.getVisibility() == View.VISIBLE) {
						button.startAnimation(AnimationUtils.loadAnimation(
								WelcomeActivity.this,
								R.anim.push_out__up_to_down));
						button.setVisibility(View.GONE);
					}
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
		mViewPager.setAdapter(new ViewPagerAdapter());
	}

	public void start(View v) {

		// Animation
		Animation animMove;

		// load the animation
		animMove = AnimationUtils.loadAnimation(getApplicationContext(),
				R.anim.move);

		// set animation listener
		animMove.setAnimationListener(this);

		imageViewSuperMan.startAnimation(animMove);

		/*	startActivity(new Intent(this, LoginActivity.class));
		finish();*/
	}

	class ViewPagerAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return LOADING_RES.length;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// TODO Auto-generated method stub
			container.removeView((View) object);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			// TODO Auto-generated method stub
			ImageView imageView = new ImageView(WelcomeActivity.this);
			imageView.setScaleType(ScaleType.CENTER_CROP);
			imageView.setImageResource(LOADING_RES[position]);
			container.addView(imageView);
			return imageView;
		}

	}

	@Override
	public void onAnimationEnd(Animation arg0) {
		// TODO Auto-generated method stub
		startActivity(new Intent(this, LoginActivity.class));
		finish();
	}

	@Override
	public void onAnimationRepeat(Animation arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onAnimationStart(Animation arg0) {
		// TODO Auto-generated method stub

	}
}
